package day10;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Scanner;

public class Test05 {
	static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
	/*
	 * 题目：有五个学生，每个学生有3门课的成绩，
	 * 从键盘输入以上数据（包括学生号，姓名，三门课成绩），
	 * 计算出平均成绩，将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
	 * */
	System.out.println("请输入学生信息");
	//demo();
	demo1();
	
	
	
}
private static void demo() {
	Student stu0=new Student();
	Student stu1=new Student();
	Student stu2=new Student();
	Student stu3=new Student();
	Student stu4=new Student();
for(int x=0;x<5;x++) {
		
		System.out.println("第"+(x+1)+"个同学的信息");
		System.out.println("学号");
		String id=sc.next();
		System.out.println("姓名");
		String name=sc.next();
		System.out.println("第一个成绩");
		int num1=sc.nextInt();
		System.out.println("第二个成绩");
		int num2=sc.nextInt();
		System.out.println("第三个成绩");
		int num3=sc.nextInt();
		
		switch(x) {
		case 0:
			stu0.setId(id);
			stu0.setName(name);
			stu0.setNum1(num1);
			stu0.setNum2(num2);
			stu0.setNum3(num3);
			break;
		case 1:
			stu1.setId(id);
			stu1.setName(name);
			stu1.setNum1(num1);
			stu1.setNum2(num2);
			stu1.setNum3(num3);
			break;
		case 2:
			stu2.setId(id);
			stu2.setName(name);
			stu2.setNum1(num1);
			stu2.setNum2(num2);
			stu2.setNum3(num3);
			break;
		case 3:
			stu3.setId(id);
			stu3.setName(name);
			stu3.setNum1(num1);
			stu3.setNum2(num2);
			stu3.setNum3(num3);
			break;
		case 4:
			stu4.setId(id);
			stu4.setName(name);
			stu4.setNum1(num1);
			stu4.setNum2(num2);
			stu4.setNum3(num3);
			break;
		default:
			System.out.println("失败");
		break;
		}
		
		
		
	}
	try {
		ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("stud.txt"));
		oos.writeObject(stu0);
		oos.writeObject(stu1);
		oos.writeObject(stu2);
		oos.writeObject(stu3);
		oos.writeObject(stu4);
		oos.writeObject(null);
		oos.close();
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	
}

private static void demo1() {
	try {
		ObjectInputStream ois=new ObjectInputStream(new FileInputStream("stud.txt"));
		try {
			Object leng=null;
			while((leng=ois.readObject())!=null) {
			
			System.out.println(leng);
			}
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}
}
